package com.siva.customer.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.siva.customer.domain.Customer;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/V0.1/customers")
public class CustomerCtroller {
    
    static Map<Long, Customer> customers =  Maps.newHashMap();
    
    @ApiOperation(value = "获取用户列表", notes = "")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<Customer> getCustomerList() {
        List<Customer> result = new ArrayList<Customer>(customers.values());
        return result;
    }
    
    @ApiOperation(value = "创建用户", notes="根据customer对象创建用户")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postCustomer(@ModelAttribute Customer customer) {
        customers.put(customer.getId(), customer);
        return "success";
    }
    
    @ApiOperation(value = "获取用户详细信息", notes="根据id获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType="path", dataType="long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Customer getCustomer(@PathVariable Long id) {
        return customers.get(id);
    }
    
    @ApiOperation(value = "更新用户详细信息", notes="根据id更新用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType="path", dataType="long")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @ModelAttribute Customer customer) {
        Customer cus = customers.get(id);
        cus.setName(customer.getName());
        cus.setAge(customer.getAge());
        return "success";
    }
    
    @ApiOperation(value = "删除用户信息", notes="根据id删除用户信息")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType="path", dataType="long")
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public String deleteCustomer(@PathVariable Long id) {
        customers.remove(id);
        return "success";
    }
}
